package com.bobo.service.admin;

import com.bobo.dao.admin.AdminDaoImpl;
import com.bobo.dao.admin.IAdminDao;
import com.bobo.model.Admin;
import com.bobo.util.MD5;

import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * Created by bobo on 2016/10/24.
 */
public class AdminServiceImpl implements IAdminService {

    IAdminDao adminDao = new AdminDaoImpl();
    @Override
    public Integer create(Admin admin) {
        try {
            String pwd = MD5.md5(admin.getPassword());
            admin.setPassword(pwd);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return adminDao.create(admin);
    }

    @Override
    public boolean del(Integer id) {
        return adminDao.delete(id);
    }

    @Override
    public boolean upd(Admin admin) {
        try {
            String pwd = MD5.md5(admin.getPassword());
            admin.setPassword(pwd);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return adminDao.upd(admin);
    }

    @Override
    public Admin getAdmin(Integer id) {
        return adminDao.getAdmin(id);
    }

    @Override
    public List<Admin> getAdmins() {
        return adminDao.getAdmins();
    }

    @Override
    public Admin adminCompare(String name, String password) {
        String pwd = null;
        try {
            pwd = MD5.md5(password);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        Admin admin = adminDao.adminCompare(name, pwd);
        if(admin==null){
            return null;
        }
        return admin;
    }
}
